package com.schoolComments.common;

import com.schoolComments.dto.UserDTO;
import com.schoolComments.utils.UserHolder;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 马园博
 * @date 2023/04/06/16:58
 * @Project hm-dianping
 **/
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserDTO user = UserHolder.getUser();
        if (user==null) {
            response.setStatus(401);
            return false;
        }
        return true;
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //在处理完service()后 直接移除当前线程中的user 不然就算redis里的token过期了 这个线程中的user还是能取到(因为没移除)
        //就会造成服务器那边已经下线了  但客户端还是能正常使用的bug
        UserHolder.removeUser();
    }
}
